/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ik.RestauranteBar.SMO.Ventanas;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.ParseException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.ik.RestauranteBar.SMO.Ventanas.Modelos.TableModelDirecciones;
import org.ik.RestauranteBar.SMO.Encapsulados.Direcciones;
import org.ik.RestauranteBar.SMO.Encapsulados.Tablas.Direcciones_Tabla;
import org.ik.RestauranteBar.SMO.Encapsulados.DireccionesPK;
import org.ik.RestauranteBar.SMO.Encapsulados.Usuarios;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorClientes;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorDirecciones;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorEstado;
import org.ik.RestauranteBar.SMO.Manejadores.ManejadorTiposDirecciones;

/**
 *
 * @author Malkavian
 */
public class Clientes extends javax.swing.JFrame {

    Usuarios u = new Usuarios();
    org.ik.RestauranteBar.SMO.Encapsulados.Clientes cliente = new org.ik.RestauranteBar.SMO.Encapsulados.Clientes();
    org.ik.RestauranteBar.SMO.Encapsulados.Direcciones direccion = new Direcciones();
    org.ik.RestauranteBar.SMO.Encapsulados.DireccionesPK direccion_pk = new DireccionesPK();
    List<Direcciones_Tabla> direcciones_tabla = new ArrayList<Direcciones_Tabla>();
    List<Direcciones> direcciones = new ArrayList<Direcciones>();

    public Clientes(Usuarios u) {

        this.u = u;

        initComponents();

        tabla_direcciones.setModel(new TableModelDirecciones());

        Dimension d = new Dimension(this.getSize());
        Dimension g = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(g.width / 2 - d.width / 2, g.height / 2 - d.height / 2, d.width, d.height);

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        texto_telefono = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        texto_nombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        texto_apellido = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        combo_estado = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        fechaNacimiento = new org.jdesktop.swingx.JXDatePicker();
        jLabel9 = new javax.swing.JLabel();
        clasificacion = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        correoElectronico = new javax.swing.JTextField();
        boton_busqueda_cliente = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textarea_direccion = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        boton_agregar_direccion = new javax.swing.JButton();
        boton_limpiar_direccion = new javax.swing.JButton();
        boton_borrar_direccion = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_direcciones = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        tipoDireccion = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        notaDireccion = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        boton_guardar_cliente = new javax.swing.JButton();
        boton_borrar_cliente = new javax.swing.JButton();
        boton_limpiar_cliente = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("SMO - Información Clientes");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información General", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Teléfono:");

        try{
            texto_telefono.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-###-####")));
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        texto_telefono.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_telefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                texto_telefonoKeyTyped(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Nombre:");

        texto_nombre.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_nombre.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Apellido:");

        texto_apellido.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_apellido.setEnabled(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Estado:");

        combo_estado.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_estado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Activo", "Inactivo" }));
        combo_estado.setEnabled(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Fecha Nacimiento:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Clasificación:");

        clasificacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Correo Electrónico:");

        correoElectronico.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        correoElectronico.setEnabled(false);

        boton_busqueda_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/group.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(boton_busqueda_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(jLabel2)))
                        .addGap(39, 39, 39)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(texto_telefono)
                            .addComponent(texto_nombre)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(combo_estado, 0, 157, Short.MAX_VALUE)
                            .addComponent(texto_apellido)
                            .addComponent(fechaNacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(clasificacion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(correoElectronico)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(texto_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(boton_busqueda_cliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(texto_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(texto_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(correoElectronico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(fechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(clasificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(combo_estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Direcciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Dirección:");

        textarea_direccion.setEditable(false);
        textarea_direccion.setColumns(20);
        textarea_direccion.setRows(5);
        jScrollPane2.setViewportView(textarea_direccion);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles"));

        boton_agregar_direccion.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_agregar_direccion.setText("Agregar Dirección");
        boton_agregar_direccion.setEnabled(false);
        boton_agregar_direccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_agregar_direccionActionPerformed(evt);
            }
        });

        boton_limpiar_direccion.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_limpiar_direccion.setText("Limpiar");
        boton_limpiar_direccion.setEnabled(false);
        boton_limpiar_direccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_limpiar_direccionActionPerformed(evt);
            }
        });

        boton_borrar_direccion.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_borrar_direccion.setText("Borrar Dirección");
        boton_borrar_direccion.setToolTipText("");
        boton_borrar_direccion.setEnabled(false);

        tabla_direcciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla_direcciones.setEnabled(false);
        tabla_direcciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabla_direccionesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla_direcciones);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(boton_agregar_direccion)
                        .addGap(18, 18, 18)
                        .addComponent(boton_limpiar_direccion)
                        .addGap(18, 18, 18)
                        .addComponent(boton_borrar_direccion)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton_agregar_direccion)
                    .addComponent(boton_limpiar_direccion)
                    .addComponent(boton_borrar_direccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel11.setText("Tipo Dirección:");

        tipoDireccion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel12.setText("Nota:");

        notaDireccion.setEditable(false);
        notaDireccion.setColumns(20);
        notaDireccion.setRows(5);
        jScrollPane3.setViewportView(notaDireccion);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tipoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel12))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane3))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(tipoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        boton_guardar_cliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_guardar_cliente.setText("Guardar");
        boton_guardar_cliente.setEnabled(false);
        boton_guardar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_guardar_clienteActionPerformed(evt);
            }
        });

        boton_borrar_cliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_borrar_cliente.setText("Borrar");
        boton_borrar_cliente.setEnabled(false);

        boton_limpiar_cliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_limpiar_cliente.setText("Limpiar");
        boton_limpiar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_limpiar_clienteActionPerformed(evt);
            }
        });

        jButton6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton6.setText("Cerrar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boton_guardar_cliente, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                    .addComponent(boton_borrar_cliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(boton_limpiar_cliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton_guardar_cliente)
                    .addComponent(boton_limpiar_cliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton_borrar_cliente)
                    .addComponent(jButton6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/user-group-new.png"))); // NOI18N

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ik/RestauranteBar/SMO/Recursos/stock_task.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel8)
                                .addGap(28, 28, 28)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boton_agregar_direccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_agregar_direccionActionPerformed
        if (textarea_direccion.getText().isEmpty() && tipoDireccion.getSelectedIndex()>0) {
            JOptionPane.showMessageDialog(null, "Debe escribir alguna dirección.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            org.ik.RestauranteBar.SMO.Encapsulados.Clientes cliente = ManejadorClientes.getInstance().getClientePorTelefono(texto_telefono.getText());

            if (cliente != null) {

                direccion_pk.setClientesTelefono(cliente.getTelefono());
                direccion_pk.setIddireccion(ManejadorDirecciones.getInstance().maxDireccion(cliente.getTelefono()));
                direccion.setDireccionesPK(direccion_pk);
                direccion.setDireccion(textarea_direccion.getText());
                direccion.setNota(notaDireccion.getText());
                direccion.setTiposDireccionesid(ManejadorTiposDirecciones.getInstance().getTiposdirecciones(tipoDireccion.getSelectedIndex()));

                ManejadorDirecciones.getInstance().insertarDirecciones(direccion);

                direcciones = ManejadorDirecciones.getInstance().getDireccionesCliente(texto_telefono.getText());
                
                if (direcciones_tabla != null) {
                    for (int i = 0; i < direcciones_tabla.size(); i++) {
                        direcciones_tabla.remove(i);
                    }
                }

                for (Direcciones d : direcciones) {
                    Direcciones_Tabla dt = new Direcciones_Tabla();

                    dt.setIdDireccion(d.getDireccionesPK().getIddireccion());
                    dt.setDireccion(d.getDireccion());
                    dt.setTipoDireccion(d.getTiposDireccionesid().getDescripcion());
                    direcciones_tabla.add(dt);

                }

                tabla_direcciones.setModel(new TableModelDirecciones(direcciones_tabla));
            }
        }
    }//GEN-LAST:event_boton_agregar_direccionActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        Principal p = new Principal(u);
        dispose();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void boton_limpiar_direccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_limpiar_direccionActionPerformed
        textarea_direccion.removeAll();
    }//GEN-LAST:event_boton_limpiar_direccionActionPerformed

    private void boton_limpiar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_limpiar_clienteActionPerformed
        texto_telefono.setText("");
        texto_nombre.setText("");
        texto_apellido.setText("");
        combo_estado.setSelectedIndex(0);

        texto_apellido.setEnabled(true);
        texto_nombre.setEnabled(true);
        textarea_direccion.setEnabled(true);
        boton_agregar_direccion.setEnabled(true);
        boton_borrar_cliente.setEnabled(true);
        boton_guardar_cliente.setEnabled(true);
        boton_limpiar_direccion.setEnabled(true);
        boton_borrar_direccion.setEnabled(true);
        combo_estado.setEnabled(true);
        tabla_direcciones.setEnabled(true);
    }//GEN-LAST:event_boton_limpiar_clienteActionPerformed

    private void texto_telefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_texto_telefonoKeyTyped
        if (evt.getKeyChar() == '\n') {

            if (texto_telefono.getText().toCharArray().length == 12) {

                cliente = ManejadorClientes.getInstance().getClientePorTelefono(texto_telefono.getText());

                if (cliente != null) {

                    texto_nombre.setText(cliente.getNombre().trim());
                    texto_apellido.setText(cliente.getApellido().trim());

                    if (cliente.getEstadosid().getId()==1) {
                        combo_estado.setSelectedIndex(0);
                    } else {
                        combo_estado.setSelectedIndex(1);
                    }

                    direcciones = ManejadorDirecciones.getInstance().getDireccionesCliente(texto_telefono.getText());

                    if (direcciones_tabla != null) {
                        for (int i = 0; i < direcciones_tabla.size(); i++) {
                            direcciones_tabla.remove(i);
                        }
                    }

                    for (Direcciones d : direcciones) {
                        Direcciones_Tabla dt = new Direcciones_Tabla();

                        dt.setIdDireccion(d.getDireccionesPK().getIddireccion());
                        dt.setDireccion(d.getDireccion());
                        dt.setTipoDireccion(d.getTiposDireccionesid().getDescripcion());
                        direcciones_tabla.add(dt);

                    }

                    tabla_direcciones.setModel(new TableModelDirecciones(direcciones_tabla));

                    texto_apellido.setEnabled(true);
                    texto_nombre.setEnabled(true);
                    textarea_direccion.setEnabled(true);
                    boton_agregar_direccion.setEnabled(true);
                    boton_borrar_cliente.setEnabled(true);
                    boton_guardar_cliente.setEnabled(true);
                    boton_limpiar_direccion.setEnabled(true);
                    boton_borrar_direccion.setEnabled(true);
                    combo_estado.setEnabled(true);
                    tabla_direcciones.setEnabled(true);


                } else {
                    JOptionPane.showMessageDialog(null, "Teléfono no existe.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "Teléfono no existe.", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }
    }//GEN-LAST:event_texto_telefonoKeyTyped

    private void tabla_direccionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabla_direccionesMouseClicked
        if (tabla_direcciones.getSelectedRow() >= 0) {
            TableModelDirecciones table_direcciones = (TableModelDirecciones) tabla_direcciones.getModel();
            Direcciones_Tabla direccion_seleccionado =
                    (Direcciones_Tabla) table_direcciones.getRow(tabla_direcciones.getSelectedRow());

            textarea_direccion.setText(direccion_seleccionado.getDireccion());
        }
    }//GEN-LAST:event_tabla_direccionesMouseClicked

    private void boton_guardar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_guardar_clienteActionPerformed

        if (texto_apellido.getText().isEmpty() || texto_nombre.getText().isEmpty() ||  texto_telefono.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Favor llenar todas las informaciones.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            cliente.setApellido(texto_apellido.getText());
            cliente.setApellido(texto_nombre.getText());
            cliente.setApellido(texto_telefono.getText());

            if (combo_estado.getSelectedIndex() == 0) {
                cliente.setEstadosid(ManejadorEstado.getInstance().getEstados(1));
            } else {
                cliente.setEstadosid(ManejadorEstado.getInstance().getEstados(0));
            }

            cliente.setFechaIngreso(new Date());

            ManejadorClientes.getInstance().insertarClientes(cliente);

            boton_limpiar_cliente.doClick();
            boton_limpiar_direccion.doClick();

        }


    }//GEN-LAST:event_boton_guardar_clienteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_agregar_direccion;
    private javax.swing.JButton boton_borrar_cliente;
    private javax.swing.JButton boton_borrar_direccion;
    private javax.swing.JButton boton_busqueda_cliente;
    private javax.swing.JButton boton_guardar_cliente;
    private javax.swing.JButton boton_limpiar_cliente;
    private javax.swing.JButton boton_limpiar_direccion;
    private javax.swing.JComboBox clasificacion;
    private javax.swing.JComboBox combo_estado;
    private javax.swing.JTextField correoElectronico;
    private org.jdesktop.swingx.JXDatePicker fechaNacimiento;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextArea notaDireccion;
    private javax.swing.JTable tabla_direcciones;
    private javax.swing.JTextArea textarea_direccion;
    private javax.swing.JTextField texto_apellido;
    private javax.swing.JTextField texto_nombre;
    private javax.swing.JFormattedTextField texto_telefono;
    private javax.swing.JComboBox tipoDireccion;
    // End of variables declaration//GEN-END:variables

}
